package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
//import com.itheima.reggie.entity.DishDto;
//菜品管理
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    @Autowired(required = false)
    private DishService dishService;
    @Autowired(required = false)
    private DishFlavorService dishFlavorService;

    /**
     * 作用：保存菜品并且携带口味信息
     *
     * @param dishDto 用于接收菜品与口味信息
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto, HttpSession session) {
        //获取登入者信息
        Long empId = (Long) session.getAttribute("employee");
        dishDto.setCreateUser(empId);
        dishDto.setUpdateUser(empId);
        //保存添加菜品信息
        dishService.saveWithFlavor(dishDto);
        return R.success("保存成功");
    }

    /**
     * 菜品信息分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>> page(Integer page, Integer pageSize, String name) {
        Page<DishDto> pageResult = dishService.page(page, pageSize, name);
        return R.success(pageResult);
    }

    /**
     * 作用：根据id查找菜品和菜品的口味
     *
     * @param id 菜品的id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable("id") Long id) {
        DishDto dishDto = dishService.findById(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto, HttpSession session) {
        //修改登入者的时间，设置当前修改人
        Long empId = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(empId);

        dishService.updateWithFlavor(dishDto);
        return  R.success("修改成功");
    }
    /**
     * 根据菜品类别id 查找菜品*/
    @GetMapping("/list")
    public R<List<DishDto>> list(Long categoryId,Integer status){
       List<DishDto> dishList =  dishService.findBycateoryId(categoryId,status);
       return R.success(dishList);
    }
    /*根据id删除菜品*/
    @DeleteMapping
    public R<String> Delete(@RequestParam List<Long> ids){
         dishService.DeleteByIds(ids);
         return R.success("删除成功");
    }
    /*批量停售*/
    @PostMapping("/status/{status}")
    public R<String> statusByIds(@RequestParam List<Long> ids,@PathVariable Integer status){
        dishService.updateStatusByIds(ids,status);
        return R.success("修改成功");
    }
}
